package com.example.dllo.exe.giftspeak.jingxuan;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.exe.bean.JingXuanBean;
import com.example.dllo.exe.bean.JingXuanDangHangBean;
import com.example.dllo.exe.bean.JingXuanTurnBean;
import com.example.dllo.exe.R;
import com.example.dllo.exe.giftspeak.songnvpiao.SongNvPiaoSecondTopAty;
import com.example.dllo.exe.main.BaseFragment;

import com.example.dllo.exe.fengzhuang.DBValue;
import com.google.gson.Gson;

import java.util.ArrayList;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class JingXuanFragment extends BaseFragment {

    private String turnurl = "http://api.liwushuo.com/v2/banners?channel=IOS";//轮播图的接口
    private String urlDaohang = "http://api.liwushuo.com/v2/secondary_banners?gender=2&generation=1";//导航的接口
    private ArrayList<JingXuanBean> arrayList;
    //    private ListView jingxuanListView;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private ImageView[] tips;
    private ViewPager viewPager;
    private JingXuanTurnBean jingXuanTurnBean;
    private JingXuanTurnPagerAdapter jingXuanTurnPagerAdapter;

    private boolean mm = true;
    private boolean flags = true;
    private Handler handler;
    private RecyclerView recyclerView_daoHang;
    private LinearLayout llturn;
    private MyListView jingxuanListView;
    private JingXuanAdapter jingxuanAdapter;


    @Override
    protected int setLayout() {
        return R.layout.item_mylistview;

    }

    @Override
    protected void initView() {

        jingxuanListView = bindView(R.id.mylistview_id);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.jingxuanturnfragmentlayout, null);
        recyclerView_daoHang = (RecyclerView) view.findViewById(R.id.jingxuan_RecycleView);
        llturn = (LinearLayout) view.findViewById(R.id.ll_turn);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        jingXuanTurnPagerAdapter = new JingXuanTurnPagerAdapter(getContext());


        jingxuanListView.addHeaderView(view);


        jingXuanTurnBean = new JingXuanTurnBean();


        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);


                return false;
            }
        });
        if (mm) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (flags) {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(0);
                    }
                }
            }).start();
            mm = false;
        }
//       Message message=handler.obtainMessage();
//        message.what=1;
//        //可以把 msg 在延迟多少毫秒后 才被handler的callback执行
//        handler.sendMessageDelayed(message,1000);
    }


    @Override
    protected void initData() {


        arrayList = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getContext());
        //解析
        stringRequest = new StringRequest(turnurl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                jingXuanTurnBean = gson.fromJson(response, JingXuanTurnBean.class);
                jingXuanTurnPagerAdapter = new JingXuanTurnPagerAdapter(getContext());
                jingXuanTurnPagerAdapter.setImags(jingXuanTurnBean);
                viewPager.setAdapter(jingXuanTurnPagerAdapter);
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        for (int i = 0; i < jingXuanTurnBean.getData().getBanners().size(); i++) {
                            if (i == position % jingXuanTurnBean.getData().getBanners().size()) {
                                //选中的小白点
                                tips[i].setImageResource(R.mipmap.btn_check_normal);
                            } else {
                                //未选中的
                                tips[i].setImageResource(R.mipmap.btn_check_disabled_nightmode);
                            }
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                try {
                    tips = new ImageView[jingXuanTurnBean.getData().getBanners().size()];
                    for (int i = 0; i < jingXuanTurnBean.getData().getBanners().size(); i++) {
                        ImageView imageView = new ImageView(getContext());
                        imageView.setLayoutParams(new LinearLayout.LayoutParams(20, 20));
                        tips[i] = imageView;
                        if (i == 0) {
                            imageView.setImageResource(R.mipmap.btn_check_normal);
                        } else {
                            imageView.setImageResource(R.mipmap.btn_check_disabled_nightmode);
                        }
                        LinearLayout.LayoutParams layoutParms = new LinearLayout.LayoutParams(30, 30);
                        layoutParms.leftMargin = 20;
                        layoutParms.rightMargin = 20;
                        llturn.addView(imageView, layoutParms);
                    }
                    jingXuanTurnPagerAdapter.setTips(tips);
                } catch (Exception e) {

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.add(stringRequest);
        stringRequest = new StringRequest(DBValue.GIFTSPEAK_JINGXUAN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("MainAty", response);
//                Toast.makeText(getContext(), "请求成功", Toast.LENGTH_SHORT).show();
                //解析
                Gson gson = new Gson();
                final JingXuanBean jingxuanBean = gson.fromJson(response, JingXuanBean.class);
                jingxuanAdapter = new JingXuanAdapter(getContext());

                jingxuanAdapter.setJingXuanBeen(jingxuanBean);
                jingxuanListView.setAdapter(jingxuanAdapter);
                jingxuanAdapter.setItemonClick(new JingXuanAdapter.Itemonclick() {
                    @Override
                    public void Itemonclick(int pos) {


                        Intent intent = new Intent(getContext(), SongNvPiaoSecondTopAty.class);
                        intent.putExtra("key",jingxuanBean.getData().getItems().get(pos).getUrl());
                        startActivity(intent);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }

        });
        requestQueue.add(stringRequest);
        stringRequest = new StringRequest(urlDaohang, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                JingXuanDangHangBean jingXuanDangHangBean = gson.fromJson(response, JingXuanDangHangBean.class);
                JingXuanRVAdapter jingXuanRVAdapter = new JingXuanRVAdapter(getContext());
                jingXuanRVAdapter.setJingXuanDangHangBean(jingXuanDangHangBean);
                //下面是网格布局
                GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                //下面是线性布局
//               LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, flags);
                recyclerView_daoHang.setLayoutManager(manager);
                recyclerView_daoHang.setAdapter(jingXuanRVAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(stringRequest);

        jingxuanListView.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {

                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        jingxuanAdapter.notifyDataSetChanged();
                        jingxuanListView.onRefreshComplete();
                    }
                }.execute(null, null, null);
            }
        });


    }


}
